<?php
/**
 * @Author: Xu Liu email:2867557054@qq.com
 * @Date:   2016-12-15 13:31:33
 * @Last Modified by:   Xu Liu
 * @Last Modified time: 2018-01-03 21:12:57
 */
if (!defined('IN_ADMINCP') || !defined('IN_DISCUZ')) {
    exit('Access Denied');
}
class MpiaoContrl
{
    /**
     * type=101 月票 102 推荐票 103 催更票 image=>积分类型 url购买单价 adv 提现额度
     * type=201 申请记录  name 票据名称  descro 用户会员名 url 用户uid  book_1票据张数 book_2可兑换金额 image 用户附言 adv 票据类型
     *
     */
    public function index()
    {
        global $_G;
        // 获取月票设置
        $currentpage = $_GET['page']?intval($_GET['page']):1;
        $pagesize = 50;
        if ($_GET['piaotype'] == 'piaojushezhi') {
            $piaojus = DB::fetch_all('SELECT * FROM %t WHERE ' . DB::field('type', array(101, 102, 103), 'in'), array('jamesonread_topics'));
        } else if ($_GET['piaotype'] == 'tixianshenqing') {
            $shenqings = C::t('#jameson_read#jamesonread_topics')->fetch_by_type(201);
        } else if ($_GET['piaotype'] == 'goumaijilu') {
            $counts     = DB::result_first('SELECT count(id) FROM %t WHERE type=%d', array('jamesonread_piao', 1));
            $pageshow = multi($counts,$pagesize,$currentpage,ADMINSCRIPT.'?action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mpiao&act=index&piaotype=goumaijilu');
            $goumais  = C::t('#jameson_read#jamesonread_piao')->fetch_buy_all(1, $pagesize*($currentpage-1), $pagesize);
        } else if ($_GET['piaotype'] == 'tixianjilu') {
            $counts     = DB::result_first('SELECT count(id) FROM %t WHERE type=%d', array('jamesonread_piao', 2));
            $pageshow = multi($counts,$pagesize,$currentpage,ADMINSCRIPT.'?action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mpiao&act=index&piaotype=tixianjilu');
            $tixians  = C::t('#jameson_read#jamesonread_piao')->fetch_buy_all(2, $pagesize*($currentpage-1), $pagesize);
        }
        include template('jameson_read:mpiao_index');
    }

    public function post()
    {
        global $_G;
        if (submitcheck('editsubmit')) {
            foreach ($_GET['goumaijiage'] as $key => $value) {
                $data          = array();
                $data['image'] = intval($_GET['jifenleixing'][$key]); //积分类型
                $data['url']   = intval($value); //购买加个
                $data['adv']   = intval($_GET['tixianjiage'][$key]); //提现加个
                C::t('#jameson_read#jamesonread_topics')->update($key, $data);
            }
            cpmsg(lang('plugin/jameson_read', 'gengxinchenggong'), 'action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mpiao&act=index&piaotype=piaojushezhi', 'succeed');
        } else {
            cpmsg(lang('plugin/jameson_read', 'laiyuanfeifa'), 'action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mpiao&act=index&piaotype=piaojushezhi', 'error');
        }
    }
    /**
     * type=1通过 type=2拒绝
     * @param string $value [description]
     */
    public function action()
    {
        if (FORMHASH == $_GET['formhash']) {
            $type     = intval($_GET['type']);
            $message  = trim($_GET['message']);
            $topic_id = intval($_GET['topic_id']);
            $info     = C::t('#jameson_read#jamesonread_topics')->fetch(($topic_id));
            if ($info['adv'] == 101) {
                $field = 'yuepiao';
            } else if ($info['adv'] == 102) {
                $field = 'tuijianpiao';
            } else {
                $field = 'cuigengpiao';
            }
            if ($type == 1) {
                // 通过插入记录表
                C::t('#jameson_read#jamesonread_piao')->insert(array(
                    'uid'      => $info['url'],
                    'username' => $info['descro'],
                    'addtime'  => TIMESTAMP,
                    'exid'     => $info['image'],
                    'name'     => $info['name'],
                    'number'   => $info['book_1'],
                    'money'    => $info['book_2'],
                    'type'     => 2,
                ));
                C::t('#jameson_read#jamesonread_authors')->update($info['url'], array($field => 0));
                jameson_sendsms($info['url'], 0, 0, lang('plugin/jameson_read', 'shenqingtongguo'), 0);
            } else {
                $message = $message ? $message : '';
                jameson_sendsms($info['url'], 0, 0, lang('plugin/jameson_read', 'shenqingjujue') . $message, 0);
            }
            C::t('#jameson_read#jamesonread_topics')->delete($topic_id);
            cpmsg(lang('plugin/jameson_read', 'caozuochenggong'), 'action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mpiao&act=index&piaotype=tixianshenqing', 'succeed');
        } else {
            cpmsg(lang('plugin/jameson_read', 'laiyuanfeifa'), 'action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mpiao&act=index&piaotype=tixianshenqing', 'error');
        }
    }
}